/**
 *  ----------------------------------------------------------------------------
 *  WirelessTest.ino - test transceiver sketch using AIR430Boost ETSI driver.
 *  Copyright (C) 2012-2013 Anaren Microwave, Inc.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 * 
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 *  This example demonstrates usage of the AIR430BoostETSI library which uses
 *  the 430Boost-CC110L AIR Module BoosterPack created by Anaren Microwave, Inc.
 *  and available through the TI eStore, for the European Union.
 *
 *  ----------------------------------------------------------------------------
 *
 *  Note: This file is part of AIR430Boost.
 *
 *  ----------------------------------------------------------------------------
 *
 *  Description
 *  ===========
 *
 *  Each radio will send a message consisting of: 1 byte counter, 5 byte static 
 *  text. The counter will count from 0 to 9 and will rollover. Each radio will 
 *  wait in receive mode for approximately one second. Upon receiving data, or 
 *  timeout of one second, the radio receive function will return. If valid data 
 *  was received, the radio's receiverOn() method will return the number of bytes
 *  that were received. In this example, the data can be monitored on the serial 
 *  port (please refer to printTxData() and printRxData() functions).
 *
 *  ----------------------------------------------------------------------------
 * 
 *  This example assumes that two BoosterPacks will be used to showcase the 
 *  wireless radio communication functionality. This same code should be 
 *  programmed to both LaunchPad development kits.
 *
 *  This BoosterPack relies on the SPI hardware peripheral and two additional 
 *  GPIO lines for SPI chip-select and GDO0 for packet handling. They use pins 18 
 *  and 19 respectively. 
 *
 *  In the default configuration, this BoosterPack is not compatible with an 
 *  external crystal oscillator. This can be changed, if necessary, and would
 *  require reconfiguration of the BoosterPack hardware and changes to the 
 *  AIR430BoostETSI library. Refer to the BoosterPack User's Manual if necessary.
 *
 *  ETSI regulations must be followed when using this library. This example 
 *  limits the wireless duty cycle to 0.1% per ETSI.
 *
 *  For complete information, please refer to the BoosterPack User's Manual 
 *  available at:
 *  https://www.anaren.com/air/cc110l-air-module-boosterpack-embedded-antenna-module-anaren
 *  
 *  To purchase the 430Boost-CC110L AIR module BoosterPack kit, please visit the 
 *  TI eStore at:
 *  https://estore.ti.com/430BOOST-CC110L-CC110L-RF-Module-BoosterPack-P2734.aspx
 */

// The AIR430BoostETSI library uses the SPI library internally. Energia does not
// copy the library to the output folder unless it is referenced here.
// The order of includes is also important due to this fact.
#include <SPI.h>
#include <AIR430BoostETSI.h>

const int analogInPin1 = A3;  
const int analogInPin3 = A5;
int sensorValue1 = 0; // value read from the pot
int sensorValue3 = 0; // value read from the pot
int outputValue1 = 0;        // value output to the PWM (analog out)
int outputValue3 = 0;        // value output to the PWM (analog out)
 
 
  
// -----------------------------------------------------------------------------
/**
 *  Global data
 */

// Data to write to radio TX FIFO (60 bytes MAX.)
unsigned char txData[1]={'A'};
unsigned char txData1[1]={'B'};
unsigned char txData2[1]={'C'};
unsigned char txData3[1]={'D'};




// -----------------------------------------------------------------------------
// Debug print functions




// -----------------------------------------------------------------------------
// Main example

void setup()
{
  
  
  // The radio library uses the SPI library internally, this call initializes
  // SPI/CSn and GDO0 lines. Also setup initial address, channel, and TX power.
  Radio.begin(0x01, CHANNEL_1, POWER_MAX);

  // Setup serial for debug printing.
  Serial.begin(9600);

 
  
  //pinMode(RED_LED, OUTPUT);
  //digitalWrite(RED_LED, LOW);   // set the LED on
}

void loop()
{
  sensorValue1 = analogRead(analogInPin1);
  
  sensorValue3 = analogRead(analogInPin3);  

  outputValue1 = map(sensorValue1, 0, 1023, 0, 255);  
   outputValue1=outputValue1/.5;
  outputValue3 = map(sensorValue3, 0, 1023, 0, 255);  
   outputValue3=outputValue3/.5;  

  // print the results to the serial monitor:
  Serial.print("X= " );                            
  Serial.println(outputValue1);   
  Serial.print("Z= " );                            
  Serial.println(outputValue3); 
  
  // Load the txData into the radio TX FIFO and transmit it to the broadcast
  // address.
  if(154<outputValue1<170)
  {
     Radio.transmit(ADDRESS_BROADCAST, txData, 1);
  }
   
  
   else if(190<outputValue1<200)
   {
     Radio.transmit(ADDRESS_BROADCAST, txData1, 1);
   }
   
   if(158<outputValue1<168)
   {
    Radio.transmit(ADDRESS_BROADCAST, txData2, 1);
   }
   
   else if(188<outputValue1<202)
   {
    Radio.transmit(ADDRESS_BROADCAST, txData3, 1);
   }
 
  /**
   *  The radio transmitter and receiver cannot be operated at the same time.
   *  Wait until transmit completes before turning on the receiver. Please note
   *  that the radio is considered busy when it is transmitting.
   *
   *  WARNING: If busy is not checked between two successive radio operations
   *  receiverOn/transmit, the radio may not perform the specified task. The
   *  radio must be complete with the transmission before it can begin the next
   */
  
  
  /**
   *  Warning: ETSI regulations require duty cycling of 0.1% per hour. Changing this
   *  may invalidate ETSI compliance. Some margin has been added. Please refer to
   *  Anaren's A110LR09 User's Manual for more information.
   */
  delay(500);
}
